package com.toman.ui.work.workshop;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.text.InputType;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.LinearLayout;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;
import com.lzy.okhttputils.request.PostRequest;
import com.toman.Enum.ViewTypeEnum;
import com.toman.Interfaces.NewBaseUrl;
import com.toman.JavaBean.NoplanReportDto;
import com.toman.R;
import com.toman.Util.JsonUtils;
import com.toman.Util.ToastUtil;
import com.toman.Util.ViewUtil;
import com.toman.bean.TemplateInfBean;
import com.toman.camera.Camera;
import com.toman.templateActivity.controler.TempInfAdapter;
import com.toman.templateActivity.controler.TempView;
import com.toman.toolbar.ToolBarActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Request;
import okhttp3.Response;

public class NoPlanCheckActivity extends ToolBarActivity {
    NoplanReportDto bean = new NoplanReportDto();
    @BindView(R.id.ll)
    LinearLayout ll;
    private TempInfAdapter adapter;
    List<TemplateInfBean> list = new ArrayList<>();
    Camera camera;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_template_inf);
        ButterKnife.bind(this);
        initToolBar("无计划报工审核");
        initIntent();
        initView();
    }

    private void initView() {
        list.add(new TemplateInfBean(null, "提交完工数", bean.getCompletedCount() + "", null, ViewTypeEnum.EDIT, InputType.TYPE_CLASS_NUMBER, false, false, true));
        list.add(new TemplateInfBean(null, "提交合格数", bean.getQualifiedCount() + "", null, ViewTypeEnum.EDIT, InputType.TYPE_CLASS_NUMBER, false, false, true));
        list.add(new TemplateInfBean("verifyCompletedCount", "审核完工数", bean.getCompletedCount() + "", bean.getCompletedCount() + "", ViewTypeEnum.EDIT, InputType.TYPE_CLASS_NUMBER, false, true, true));
        list.add(new TemplateInfBean("verifyQualifiedCount", "审核合格数", bean.getQualifiedCount() + "", bean.getQualifiedCount() + "", ViewTypeEnum.EDIT, InputType.TYPE_CLASS_NUMBER, false, true, true));
        adapter = new TempInfAdapter(list,this,ll,true);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.toolbar_finish, menu);

        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.menu_finish:
                        add();
                        break;
                }
                return false;
            }
        });

        return true;
    }

    private void initIntent() {
        bean = (NoplanReportDto) getIntent().getSerializableExtra("bean");
    }

    public void add() {

        if (adapter.isNull()) {
            return;
        }
        ;
        if (TempView.compareString(this, list.get(2), list.get(3))
                ) {
            return;
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id",bean.getId());
        for (TemplateInfBean bean : list) {
            if (bean.getName() != null && bean.getType() != ViewTypeEnum.PHOTO) {
                map.put(bean.getName(), bean.getResult());
            }
        }
        String str = JsonUtils.obj2JsonString(map);
        PostRequest request = OkHttpUtils
                .post(NewBaseUrl.devicehouse_noplanreport_appverify)
                .headers("Content-Type", "application/json")
                .headers("token", token);

        request.postJson(str);
        request.execute(new StringCallback() {
            @Override
            public void onResponse(boolean isFromCache, String result, Request request, @Nullable Response response) {
                Log.i("test", getClass().toString() + result);
                JSONObject jsonObj = JSON.parseObject(result);
                int code = jsonObj.getInteger("code");
                ViewUtil.lagout(code, getApplicationContext());

                if (code == 0) {
                    ToastUtil.makeText(getApplicationContext(), jsonObj.getString("msg"));
                    finish();

                } else {
                    ToastUtil.error(getApplicationContext(), jsonObj.getString("msg"));
                }

            }
        });

    }


}
